package main

import (
	"context"
	"os"
	"os/signal"
	"telegram-bot-c9121/internal/config"
	"telegram-bot-c9121/internal/logger"

	"github.com/go-telegram/bot"
	"github.com/go-telegram/bot/models"
)

func main() {
	logger.Info("Starting...")

	ctx := context.Background()

	config, err := config.New()
	if err != nil {
		logger.Fatal("Failed to load config", err)
	}

	logger.Debug("Config loaded")

	ctx, cancel := signal.NotifyContext(ctx, os.Interrupt)
	defer cancel()

	opts := []bot.Option{
		bot.WithDebug(),
		bot.WithDefaultHandler(handler),
	}

	b, err := bot.New(config.BotToken(), opts...)
	if err != nil {
		logger.Fatal("Failed to create bot", err)
	}

	b.Start(ctx)
	logger.Info("Bot started")
}

func handler(ctx context.Context, b *bot.Bot, update *models.Update) {
	b.SendMessage(ctx, &bot.SendMessageParams{
		ChatID: update.Message.Chat.ID,
		Text:   update.Message.Text,
	})
}
